from django.db import models
from sync.models.sf_object import SFObject

class SFChildRelation(models.Model):
    obj=models.ForeignKey(SFObject)
    cascadeDelete=models.BooleanField()
    childSObject=models.CharField(max_length=255,null=True)
    deprecatedAndHidden=models.BooleanField()
    field=models.CharField(max_length=255,null=True)
    class Meta:
        db_table = 'sync_child_relation'
        app_label = 'sync'
    @staticmethod
    def set_relationship(sf_object,r):
        childSObject =getattr(r,'childSObject', "")
        created=False
        try:
            relation=SFChildRelation.objects.get(obj=sf_object,childSObject=childSObject )
        except:
            relation=SFChildRelation(obj=sf_object,childSObject=childSObject )
            created=True
        relation.cascadeDelete= getattr(r,'cascadeDelete', False) 
        relation.deprecatedAndHidden= getattr(r,'deprecatedAndHidden', False) 
        relation.field= getattr(r,'field', "")
        relation.save()
        return created 
             